<?php
class Application_Model_problem extends Application_Model_DomainObject{
	
        protected $_id;//序号
        protected $_userid;//用户id 
        protected $_type;//类型
        protected $_issuetype;//问题类型
        protected $_glid;//关联id
        protected $_title;//问题标题
        protected $_content;//问题内容
        
        const TYPE_BILL = 1;//运单
        
        const TYPE_CHARGE = 2;//代充值

        const TYPE_AGENTPAY = 3;//代支付

        public static function byaddissue($glid,$title,$content,$issuetype,$userid,$message,$mail,$type = Application_Model_problem::TYPE_BILL)
        {
            if($type == Application_Model_problem::TYPE_BILL){
                $billItem = Application_Model_wayBillItem::load($glid);
                if($billItem->status == Application_Model_wayBillItem::STATUS_Cancel || $billItem->status == Application_Model_wayBillItem::STATUS_Complated ){
                    return false;
                }
                Application_Model_Operate::intooperate(0, Application_Model_Operate::Type_billproblem, $billItem->billNo, $billItem->userId);
                $billItem->status = Application_Model_wayBillItem::STATUS_ExpressBack;
                $billItem->problem = 1;
                $billItem->save();
                if($message == "message"){
                    Application_Model_Operate::intooperate(0, Application_Model_Operate::Type_billmessage, $billItem->billNo, $billItem->userId);
                }
                if($mail == "mail"){
                    Application_Model_Operate::intooperate(0, Application_Model_Operate::Type_billmail, $billItem->billNo, $billItem->userId);
                }
                $where[' type = '] = $type;
                $where[' and glid = '] = $glid;
                $result = Application_Model_problem::getDao()->queryOne($where);
                if(!$result){
                    $result = new Application_Model_problem();
                }      
                $result->userid = $userid;
                $result->glid = $glid;
                $result->type = $type;
                $result->issuetype = $issuetype;
                $result->title = $title;
                $result->content = $content;
                $result->save();
            }else if($type == Application_Model_problem::TYPE_CHARGE){  //代充值
                $chargeItem = Application_Model_topUpAlipay::load($glid);
                //已处理和未付款的代充值不能退款
                if($chargeItem->status == Application_Model_topUpAlipay::STATUS_Sended || $chargeItem->status == Application_Model_topUpAlipay::STATUS_WaitPay){
                    return false;
                }
                $chargeItem->status = Application_Model_topUpAlipay::STATUS_Refund;
                $chargeItem->dateProcee = date("Y-m-d H:i:s");
                $chargeItem->save();
                Application_Model_Operate::intooperate(0, Application_Model_Operate::Type_chargeproblem, $chargeItem->id, $chargeItem->userId);
                if($message == "message"){
                    Application_Model_Operate::intooperate(0, Application_Model_Operate::Type_chargemessage,  $chargeItem->id, $chargeItem->userId);
                }
                if($mail == "mail"){
                    Application_Model_Operate::intooperate(0, Application_Model_Operate::Type_chargemail,  $chargeItem->id, $chargeItem->userId);
                }
                $where[' type = '] = $type;
                $where[' and glid = '] = $glid;
                $result = Application_Model_problem::getDao()->queryOne($where);
                if(!$result){
                    //金额返还
                    $userdetail = Application_Model_UserAccount::load($userid);
                    if(!$userdetail){
                        $userdetail = new Application_Model_UserAccount();
                        $userdetail->id = $userid;
                    }
                    $beforeMoney = $userdetail->money?$userdetail->money:0;
                    $userdetail->money = $userdetail->money+$chargeItem->amount;
                    $afterMoney = $userdetail->money;
                    $userdetail->save();
                    //生成记录
                    $history = new Application_Model_UserAccountHistory();
                    $history->type = 1;
                    $history->beforeMoney = $beforeMoney;
                    $history->afterMoney = $afterMoney;
                    $history->comment = '代充值：'.$chargeItem->id.'手续费及退单费';
                    $history->dateCreated = date("Y-m-d H:i:s");
                    $history->relaseId = $glid;
                    $history->state = 10;
                    $history->accountId = $userid;
                    $history->save();
                    Application_Model_Operate::intooperate(($chargeItem->amount+$chargeItem->serviceFee), Application_Model_Operate::Type_chargerefund, $chargeItem->id, $chargeItem->userId);
                    $result = new Application_Model_problem();
                }      
                $result->userid = $userid;
                $result->glid = $glid;
                $result->type = $type;
                $result->issuetype = $issuetype;
                $result->title = $title;
                $result->content = $content;
                $result->save();
            }else if($type == Application_Model_problem::TYPE_AGENTPAY){  //代支付
                $agentpayItem = Application_Model_AgentPay::load($glid);
                //未付款和已处理的代支付不能退款
                if($agentpayItem->status == Application_Model_AgentPay::STATUS_nopay || $agentpayItem->status == Application_Model_AgentPay::STATUS_prpay){
                    return false;
                }
                $agentpayItem->status = Application_Model_AgentPay::STATUS_Refund;
                $agentpayItem->dateProcee = date("Y-m-d H:i:s");
                $agentpayItem->save();
                Application_Model_Operate::intooperate(0, Application_Model_Operate::Type_agentproblem, $agentpayItem->id, $agentpayItem->memberId);
                if($message == "message"){
                    Application_Model_Operate::intooperate(0, Application_Model_Operate::Type_agentmessage, $agentpayItem->id, $agentpayItem->memberId);
                }
                if($mail == "mail"){
                    Application_Model_Operate::intooperate(0, Application_Model_Operate::Type_agentmail, $agentpayItem->id, $agentpayItem->memberId);
                }
                $where[' type = '] = $type;
                $where[' and glid = '] = $glid;
                $result = Application_Model_problem::getDao()->queryOne($where);
                if(!$result){
                    //金额返还
                    $userdetail = Application_Model_UserAccount::load($userid);
                    if(!$userdetail){
                        $userdetail = new Application_Model_UserAccount();
                        $userdetail->id = $userid;
                    }
                    $beforeMoney = $userdetail->money?$userdetail->money:0;
                    $userdetail->money = $userdetail->money+$agentpayItem->payMoney+$agentpayItem->processFee;
                    $afterMoney = $userdetail->money;
                    $userdetail->save();
                    //生成记录
                    $history = new Application_Model_UserAccountHistory();
                    $history->type = 1;
                    $history->beforeMoney = $beforeMoney;
                    $history->afterMoney = $afterMoney;
                    $history->comment = '代支付：'.$agentpayItem->id.'手续费及退单费';
                    $history->dateCreated = date("Y-m-d H:i:s");
                    $history->relaseId = $glid;
                    $history->state = 10;
                    $history->accountId = $userid;
                    $history->save();
                    Application_Model_Operate::intooperate(($agentpayItem->payMoney+$agentpayItem->processFee), Application_Model_Operate::Type_agentrefund, $agentpayItem->id, $agentpayItem->memberId);
                    $result = new Application_Model_problem();
                }      
                $result->userid = $userid;
                $result->glid = $glid;
                $result->type = $type;
                $result->issuetype = $issuetype;
                $result->title = $title;
                $result->content = $content;
                $result->save();
                
            }
            
            
            if($message == "message"){
                $messageItem = new Application_Model_Mail();
                $messageItem->title = $title;
                $messageItem->userId = $userid;
                $messageItem->type = 2;
                $messageItem->status = 0;
                $messageItem->message = $content;
                $messageItem->dateCreated = date("Y-m-d H:i:s");
                $messageItem->save();
            }
            if($mail == "mail"){
                $userItem = Application_Model_User::load($userid);
                Application_Model_Email::sendMail($userid, $userItem->email, $title, $content);
            }
            return true;
        }
        
        public static function byissue($glid,$type = Application_Model_problem::TYPE_BILL)
        {
            $where[' type = '] = $type;
            $where[' and glid = '] = $glid;
            return Application_Model_problem::getDao()->queryOne($where);
        }
        
        public static function byeditissue($glid,$type = Application_Model_problem::TYPE_BILL)
        {
            $where[' type = '] = $type;
            $where[' and glid = '] = $glid;
            $reslut = Application_Model_problem::getDao()->queryOne($where);
            if($reslut){
                $reslut->delete();
                $waybillItem = Application_Model_wayBillItem::load($glid);
                $status = $waybillItem->interior>Application_Model_wayBillItem::Interior_Pick?($waybillItem->interior!=Application_Model_wayBillItem::Interior_Picking?Application_Model_wayBillItem::STATUS_Sended:Application_Model_wayBillItem::STATUS_Confrimed):Application_Model_wayBillItem::STATUS_Confrimed;
                $waybillItem->status = $status;
                $waybillItem->problem = 0;
                $waybillItem->save();
                Application_Model_Operate::intooperate(0, Application_Model_Operate::Type_billnormal, $waybillItem->billNo, $waybillItem->userId);
                return 1;
            }else{
                return 2;
            }
        }
}
?>